import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const keepPwd = false
const token = JSON.parse(sessionStorage.getItem('wohome-token'))
const user = JSON.parse(localStorage.getItem('wohome-user'))

//当前标签
var tab = {
  link: '/index',
  title: '首页',
  closable: false
};
//打开着的标签
var tabs=[{
  link: '/index',
  title: '首页',
  closable: false
}];

//小区管理
var villageManage={
  villageId:'',
  villageName:'',
  buildId:'',
  buildName:'',
  unitId:'',
  unitName:''
}

//将标签缓存到sesstionState,应对用户点击刷新浏览器页面
var tabCache = sessionStorage.getItem('tab');
var tabsCache = sessionStorage.getItem('tabs');
var villageManageCache=sessionStorage.getItem('villageManage');

if(villageManageCache){
  villageManage=JSON.parse(villageManageCache);
}
if(tabCache) {
  tab = JSON.parse(tabCache);
}
if(tabsCache) {
  tabs = JSON.parse(tabsCache);
}
const state = {

  // auth 信息
  keep: false,
  token: token,
  user: user,

  // tabs 信息
  tab: tab,
  tabs: tabs,

  // notifications
  notifications: {
    show: false,
    options: {}
  },

  //小区管理
  villageManage:villageManage,

  //路由
  route: ''
};

// 状态变更
const mutations = {

  // 设置token
  SET_TOKEN(state, token) {
    state.token = token

  },
  REMOVE_TOKEN(state) {
    state.token = null
  },

  // 设置user
  SET_USER(state, user) {
    state.user = user
  },
  REMOVE_USER (state) {
    state.user = {}
  },

  //初始化tab
  INIT_TAB(state){
    let tabs=[{
      link: '/index',
      title: '首页',
      closable: false
    }];
    let tab = {
      link: '/index',
      title: '首页',
      closable: false
    }
    state.tabs=tabs;
    state.tab=tab;
    sessionStorage.setItem('tab',JSON.stringify(tab));
    sessionStorage.setItem('tabs',JSON.stringify(state.tabs));
  },
  // 设置tab
  CREATE_TAB(state, tab) {
    state.tabs.push(tab)
    state.tab = tab
    sessionStorage.setItem('tab',JSON.stringify(tab));
    sessionStorage.setItem('tabs',JSON.stringify(state.tabs));
  },
  REMOVE_TAB(state, tab) {
    let index = state.tabs.indexOf(tab)
    state.tabs.splice(index, 1)
    sessionStorage.setItem('tabs',JSON.stringify(state.tabs));
  },
  SELECT_TAB(state, tab) {
    state.tab = tab;
    sessionStorage.setItem('tab',JSON.stringify(tab));
  },
  //后退
  BACK_TAB(state){
    let index = state.tabs.indexOf(state.tab);
    if(index>0){
      state.tab = state.tabs[index-1];
      sessionStorage.setItem('tab',JSON.stringify(tab));
    }
  },
  //前进
  AHEAD_TAB(state){
    let index = state.tabs.indexOf(state.tab);
    if(state.tabs.length>index+1){
      state.tab = state.tabs[index+1];
      sessionStorage.setItem('tab',JSON.stringify(tab));
    }
  },
  //设置当前管理的小区
  SET_MANAGER_VILLAGE(state,id,name){
    state.villageManage.villageId=id;
    state.villageManage.villageName=name||'';
    sessionStorage.setItem('villageManage',JSON.stringify(state.villageManage));
  },
  //设置当前管理的楼宇
  SET_MANAGER_BUILD(state,id,name){
    state.villageManage.buildId=id;
    state.villageManage.buildName=name||'';
    sessionStorage.setItem('villageManage',JSON.stringify(state.villageManage));
  },
  //设置当前管理的单元
  SET_MANAGER_UNIT(state,id,name){
    state.villageManage.unitId=id;
    state.villageManage.unitName=name||'';
    sessionStorage.setItem('villageManage',JSON.stringify(state.villageManage));
  }

};

// 导出接口
export default new Vuex.Store({
  state,
  mutations
})
